package com.ltmonitor.web.controller;

import java.util.ArrayList;
import java.util.List;

import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.ltmonitor.entity.Alarm;
import com.ltmonitor.entity.AlarmRecord;
import com.ltmonitor.entity.TerminalCommand;
import com.ltmonitor.entity.VehicleData;
import com.ltmonitor.service.JT808Constants;
import com.ltmonitor.web.controller.command.TerminalCommandAction;
import com.ltmonitor.web.util.JsonMessage;
import com.ltmonitor.web.util.OnlineUser;


/**
 * 解除报警处理
 * @author shinion
 *
 */
@Controller
@RequestMapping("/adasAlarmsRelease")
public class AdasAlarmsReleaseAction extends TerminalCommandAction{
	
	@RequestMapping("/getView.action")
	public  String getView(Model model) {
		model.addAttribute("message", "加载成功");
		return "/command/releaseAlarm";
	}
	
	
	/**
	 * 请求处理单条非主动安全报警的页面
	 * 		请求来自主页面中双击报警数据中数据行，发出的请求
	 * @param model 
	 * @return
	 */
	@RequestMapping("/setOneAlarmProcess.action")
	public  String setOneAlarmProcess(long alarmId,long vehicleId,String processedText , Model model) {
		model.addAttribute("vehicleId", vehicleId);
		model.addAttribute("alarmId", alarmId);
		model.addAttribute("processedText", processedText);
		model.addAttribute("message", "加载成功");
		return "/alarm/setOneAlarmProcess";
	}
	
	/**
	 * 对报警记录进行批量解除报警处理
	 * @param vehicleIds 车辆id列表，暂不用
	 * @param adasAlarmIds 报警记录列表，用于更新处理状态
	 * @param alarmIds 报警索引列表，用于获取报警信息
	 * @return
	 */
	@ResponseBody
	@RequestMapping("/releaseAlarms.action")
	public JsonMessage releaseAlarms(long[] vehicleIds, long[] adasAlarmIds,long[] alarmIds,String processedRemark) {
		try {
			int processed = AlarmRecord.PROCESS_CLEAR;
			int msgId = 0x0200;
			int ackResult = 4;// 报警处理确认
			
			OnlineUser onlineUser = getOnlineUser();
			//定义链表保存返回的命令id
			List idList = new ArrayList();
			
			//根据报警总索引，发送指令
			if(alarmIds.length > 0 && onlineUser != null) {
				
				//定义命令对象
				TerminalCommand tc = new TerminalCommand();
				tc.setCmdType(JT808Constants.CMD_CLEAR_ALARM);
				
				for(long alarmId:alarmIds) {
					//根据alarmId获取alarm对象
					Alarm alarm = this.getAlarm(alarmId);
					if(alarm != null) {
						tc.setCmdData("" + alarm.getAckSn() + ";" + msgId + ";" + 4);
						tc.setVehicleId(alarm.getVehicleId());
						
						VehicleData vd = vehicleDataService.load(tc.getVehicleId());
						if (vd != null) {
							tc.setPlateNo(vd.getPlateNo());
							tc.setSimNo(vd.getSimNo());
						}
						
						tc.setUserId(onlineUser.getEntityId());
						tc.setOwner(onlineUser.getName());
						
						//发送指令
						SendCommand(tc);
						idList.add(""+tc.getEntityId());//存放指令id
					}
					
				}
				
			
			}//end alarmIds

			
			if(alarmIds.length > 0 && adasAlarmIds.length>0) {
				this.processAdasAlarms(idList,alarmIds, adasAlarmIds, processed,processedRemark);
			}

			return json(true, idList);
		} catch (Exception ex) {
			return json(false, ex.getMessage());
		}
	}


	/**
	 * aurthor:shinion
	 * date:2018-12-28
	 * 对所有的报警进行处理，包括主动安全和非主动安全报警信息
	 * @param vehicleIds 车辆id
	 * @param alarmIdsForAdas adas报警在alarm表中的id索引
	 * @param alarmIds 非主动安全报警在alarm表中的索引
	 * @return
	 */
	@ResponseBody
	@RequestMapping("/releaseAllAlarms.action")
	public JsonMessage releaseAllAlarms(long[] vehicleIds, long[] alarmIdsForAdas,long[] alarmIds,String processedRemark) {
		try {
			int processed = AlarmRecord.PROCESS_CLEAR;
			int msgId = 0x0200;
			int ackResult = 4;// 报警处理确认
			
			OnlineUser onlineUser = getOnlineUser();
			//定义链表保存返回的命令id
			List idList = new ArrayList();
			
			//根据报警总索引，发送指令
			if(alarmIds.length > 0 && onlineUser != null) {
				
				//定义命令对象
				TerminalCommand tc = new TerminalCommand();
				tc.setCmdType(JT808Constants.CMD_CLEAR_ALARM);
				
				for(long alarmId:alarmIds) {
					//根据alarmId获取alarm对象
					Alarm alarm = this.getAlarm(alarmId);
					if(alarm != null) {
						tc.setCmdData("" + alarm.getAckSn() + ";" + msgId + ";" + 4);
						tc.setVehicleId(alarm.getVehicleId());
						
						VehicleData vd = vehicleDataService.load(tc.getVehicleId());
						if (vd != null) {
							tc.setPlateNo(vd.getPlateNo());
							tc.setSimNo(vd.getSimNo());
						}
						
						tc.setUserId(onlineUser.getEntityId());
						tc.setOwner(onlineUser.getName());
						
						//发送指令
						SendCommand(tc);
						idList.add(""+tc.getEntityId());//存放指令id
					}
					
				}
				
			
			}//end alarmIds

			//遍历列表并处理非主动安全报警信息
			if(alarmIds.length > 0) {
				for(long alarmId:alarmIds) {
					this.processAlarm(alarmId, processed,processedRemark);
				}
			}
			
			//处理主动安全报警信息
			if(alarmIdsForAdas.length  >0) {
				this.processAdasAlarmByAlarmIds(idList,alarmIdsForAdas, processed,processedRemark);
			}

			return json(true, idList);
		} catch (Exception ex) {
			return json(false, ex.getMessage());
		}
	}

	
}
